package module1.petrashenko.kpi_java_training.ua;

import java.util.Scanner;

public class Palindrome {
	private static String lineOfPalindromes = "";
	private static String taskLine = "";
	private static final int MIN_LENGTH = 3;

	public static void main(String[] args) {
		Palindrome.input();
		Palindrome.countPalindrome();
		Palindrome.output();
	}

	static void countPalindrome() {
		for (int x = 0; x < taskLine.length(); x++) {
			int pointBegin = x;
			int step = x;

			while (true) {
				char currentSymbol = taskLine.charAt(x);
				int pointEnd = taskLine.indexOf(currentSymbol, ++step);
				
				if (pointEnd == -1) {
					break;
				}

				String fraction = taskLine.substring(pointBegin, pointEnd + 1);
				checkForEquality(fraction);
				step = pointEnd;
			}
		}
	}

	static void checkForEquality(String analyzingLine) {
		if ((checkForLength(analyzingLine)) && (checkForSpaces(analyzingLine))) {
			performComparison(analyzingLine);
		}
	}

	static boolean checkForLength(String analyzingLine) {
		return ((analyzingLine.length() < MIN_LENGTH) ? false : true);
	}

	static boolean checkForSpaces(String analyzingLine) {
		for (int x = 0; x <= analyzingLine.length() / 2; x++) {
			if (analyzingLine.charAt(x) == ' ') {
				return false;
			}
		}
		return true;
	}

	static void performComparison(String analyzingLine) {
		String compareLine1 = analyzingLine.substring(0, analyzingLine.length() / 2);
		String compareLine2 = "";
		int multiplicityCondition = analyzingLine.length() / 2;

		if (analyzingLine.length() % 2 != 0) {
			multiplicityCondition++;
		}

		for (int x = analyzingLine.length() - 1; x >= multiplicityCondition; x--) {
			compareLine2 += analyzingLine.charAt(x);
		}

		if (compareLine1.compareTo(compareLine2) == 0) {
			lineOfPalindromes += analyzingLine + "_";
		}
	}

	static void input() {
		Scanner inputVariable = new Scanner(System.in);
		System.out.println("enter your string for analyzing");
		taskLine = inputVariable.nextLine();
	}

	static void output() {
		String[] outputTokens;

		if (lineOfPalindromes.length() == 0) {
			System.out.println("nothing found");
		}

		else {
			lineOfPalindromes = lineOfPalindromes.substring(0, lineOfPalindromes.length() - 1);
			outputTokens = lineOfPalindromes.split("_");

			for (int x = 1; x <= outputTokens.length; x++) {
				System.out.printf("%d: %s\n", x, outputTokens[x - 1]);
			}
		}
	}
}